/**
 * 
 */
package edu.umd.clip.lm.nbest;

import java.text.ParseException;
import java.util.regex.*;

import edu.umd.clip.lm.util.*;
/**
 * @author Denis Filimonov <den@cs.umd.edu>
 *
 */
public class NbestListOneFormat extends NbestFormat {
	private static final Pattern pattern = Pattern.compile("^\\((-?[0-9]+|NaN)\\)\\s+(.*\\S|)\\s*$", Pattern.CASE_INSENSITIVE);
	public static final String HEADER = "NBestList1.0";
	
	/* (non-Javadoc)
	 * @see edu.umd.clip.lm.nbest.NbestFormat#parse(java.lang.String)
	 */
	@Override
	public NbestFormatData parse(String input) throws ParseException {
		Matcher matcher = pattern.matcher(input);
		if (matcher.matches()) {
			String score = matcher.group(1);
			String sentence = matcher.group(2);
			double logProb = ProbMath.bytelog2log10(Integer.parseInt(score));
			return new NbestFormatData(logProb, sentence);
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see edu.umd.clip.lm.nbest.NbestFormat#toString(edu.umd.clip.lm.nbest.NbestFormat.NBestFormatData)
	 */
	@Override
	public String toString(NbestFormatData data) {
		int bytelog = ProbMath.log10toBytelog(data.lmScore);
		return String.format("(%d) %s\n", bytelog, data.sentence);
	}

	@Override
	public String makeHeader() {
		return HEADER + "\n";
	}
	
}
